﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GetOrganized.Models;

namespace GetOrganized.Controllers
{
    public class TodoController : Controller
    {
        //
        // GET: /Todo/

        public ActionResult Index()
        {
            ViewData.Model = Todo.ThingsToBeDone;
            return View();
        }

        //
        // GET: /Todo/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Todo/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Todo/Create

        [HttpPost]
        public ActionResult Create(Todo todo)
        {
            try
            {
                Todo.ThingsToBeDone.Add(todo);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Todo/Edit/somethingTodo
 
        public ActionResult Edit(string title)
        {
            ViewData.Model = Todo.ThingsToBeDone.Find(todo => todo.Title == title);

            return View();
        }

        //
        // POST: /Todo/Edit/somethingToDo

        [HttpPost]
        public ActionResult Edit(string oldTitle, Todo item)
        {
            try
            {
                Todo.ThingsToBeDone.RemoveAll(todo => todo.Title == oldTitle);
                Todo.ThingsToBeDone.Add(item);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }       

        //
        // GET: /Todo/Delete/Title={name of todo}

        public ActionResult Delete(string title)
        {
            Todo.ThingsToBeDone.RemoveAll(todo => todo.Title == title);

            return RedirectToAction("Index");            
        }       
    }
}
